package com.college_career_system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.college_career_system.dao.EnterpriseMapper;
import com.college_career_system.dao.SubmitRecruitMapper;
import com.college_career_system.pojo.dto.RecruitDTO;
import com.college_career_system.pojo.po.Enterprise;
import com.college_career_system.pojo.po.Recruit;
import com.college_career_system.dao.RecruitMapper;
import com.college_career_system.pojo.po.SubmitRecruit;
import com.college_career_system.pojo.vo.RecruitVO;
import com.college_career_system.service.RecruitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college_career_system.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Empty
 * @since 2022-12-04
 */
@Service
public class RecruitServiceImpl extends ServiceImpl<RecruitMapper, Recruit> implements RecruitService {

    @Autowired
    private RecruitMapper recruitMapper;
    @Autowired
    private EnterpriseMapper enterpriseMapper;
    @Autowired
    private SubmitRecruitMapper submitRecruitMapper;

    @Override
    public JSONObject selectRecruit(HttpServletRequest request) {

        QueryWrapper<Recruit> wrapper = new QueryWrapper<>();
        wrapper.eq("enterprise_id", TokenUtil.getId(request));
        wrapper.orderByDesc("create_time");
        List<Recruit> recruits = recruitMapper.selectList(wrapper);
        return ResponseResult.success(recruits);
    }

    @Override
    public JSONObject selectRecruitById(int recruitId) {

        return ResponseResult.success(recruitMapper.selectById(recruitId));
    }

    @Override
    public JSONObject selectRecruitByCondition(HttpServletRequest request, String enterpriseType, String recruitType, int beginSalary, int endSalary, String location) {

        System.out.println("id:" + TokenUtil.getId(request));
        QueryWrapper<Recruit> wrapper = new QueryWrapper<>();
        if(! StringUtil.isEmptyOrNull(enterpriseType)) {
            wrapper.eq("etype_name", enterpriseType);
        }
        if(! StringUtil.isEmptyOrNull(recruitType)) {
            wrapper.eq("recruit_type", recruitType);
        }
        if(beginSalary > 0 && endSalary >= beginSalary) {
            wrapper.between("salary", beginSalary, endSalary);
        }
        if(! StringUtil.isEmptyOrNull(location)) {
            wrapper.eq("location", location);
        }
        wrapper.orderByDesc("create_time");

        List<Recruit> recruits = recruitMapper.selectList(wrapper);

        String uid = TokenUtil.getId(request);
        recruits.removeIf(recruit -> submitRecruitMapper.submitResume(uid, recruit.getRecruitId()) != 0);

        List<RecruitVO> recruitVOS = new ArrayList<>();
        recruits.forEach(recruit -> {
            RecruitVO recruitVO = PoVoMapper.INSTANCE.toRecruitVO(recruit);
            recruitVO.setEnterpriseName(enterpriseMapper.selectById(recruit.getEnterpriseId()).getEnterpriseName());
            recruitVO.setEnterpriseId(String.valueOf(recruit.getEnterpriseId()));
            recruitVOS.add(recruitVO);
        });

        return ResponseResult.success(recruitVOS);
    }

    @Override
    public JSONObject saveRecruit(RecruitDTO recruitDTO) {

        // TODO 不用输入企业id和类型
        if(StringUtil.hasOneEmptyOrNull(recruitDTO.getRecruitType(), recruitDTO.getRecruitDemand(), recruitDTO.getRecruitDuty(),
                recruitDTO.getEmail(), recruitDTO.getLocation(), recruitDTO.getEtypeName())) {
            return ResponseResult.failure("信息不能为空！", ResponseCode.STATUS_ERROR);
        }
        QueryWrapper<Enterprise> wrapper = new QueryWrapper<>();
        wrapper.eq("enterprise_id", Long.valueOf(recruitDTO.getEnterpriseId()));
        if(recruitDTO.getSalary() < 0 || enterpriseMapper.selectOne(wrapper) == null) {
            return ResponseResult.failure("数据不合法！", ResponseCode.STATUS_ERROR);
        }
        Recruit recruit = DtoPoMapper.INSTANCE.toRecruit(recruitDTO);
        recruit.setCreateTime(LocalDateTime.now());
        recruitMapper.insert(recruit);
        return ResponseResult.success();
    }

    @Override
    public JSONObject deleteRecruit(HttpServletRequest request, int recruitId) {

        recruitMapper.deleteById(recruitId);
        return ResponseResult.success();
    }

    @Override
    public JSONObject updateRecruit(HttpServletRequest request, RecruitDTO recruitDTO) {
        return null;
    }
}
